package pl.javarsovia.javarsovia4android.activities.partners;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.ExpandableListView;
import android.widget.TabHost;
import pl.javarsovia.javarsovia4android.adapters.ExpandablePartnersListAdapter;
import pl.javarsovia.javarsovia4android.domain.Partner;
import pl.javarsovia.javarsovia4android.domain.PartnerType;

import java.util.List;

/**
 * @author mateusz.grzechocinski@gmail.com
 * @since 2010-06-19
 */
public class PartnersTypeTabsCreator {
    private TabHost tabHost;
    private Activity activity;

    public PartnersTypeTabsCreator(TabHost tabHost, Activity activity) {
        this.tabHost = tabHost;
        this.activity = activity;
    }

    public TabHost.TabSpec createPartnerTypeTab(PartnerType type, List<Partner> partnersOfType) {
        Drawable tabIco = activity.getResources().getDrawable(type.getTabIcoResId());
        int tabContentResId = type.getTabContentResId();
        String tabEmptyLabel = "";
        fillParntersListOfGivenType(type, partnersOfType);
        return tabHost.newTabSpec(type.name()).setIndicator(tabEmptyLabel, tabIco).setContent(tabContentResId);
    }

    private void fillParntersListOfGivenType(PartnerType type, List<Partner> partnersOfType) {
        ExpandableListView list = (ExpandableListView) activity.findViewById(type.getExpandableListResId());
        ExpandablePartnersListAdapter expandablePartnersListAdapter = new ExpandablePartnersListAdapter(activity, partnersOfType);
        list.setAdapter(expandablePartnersListAdapter);
        list.expandGroup(0);
    }
}
